﻿using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using XXX.Models;
using XXX.Models.AS;

namespace XXX.Common
{
    public class AppSettings
    {
        private static IConfigurationSection appSection = null;
        /// <summary>
        /// 获取配置文件
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetAppSeting(string key)
        {
            if (appSection.GetSection(key)!=null)
            {
                return appSection.GetSection(key).Value;
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 获取配置文件
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static List<Orglist> GetAppSetinglist(string key)
        {
            if (appSection.GetSection(key) != null)
            {
               //var lst2 = appSection.GetSection(key).Get<List<Orglist>>();
                List<Orglist> lst =  appSection.GetSection(key).Get<List<Orglist>>();
                return lst;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取指定的实体信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetSectionModel<T>(string key)
        {
            if (appSection.GetSection(key) != null)
            {
                //var lst2 = appSection.GetSection(key).Get<List<Orglist>>();
                T lst = appSection.GetSection(key).Get<T>();
                return lst;
            }
            else
            {
                return default(T);
            }
        }
        /// <summary>
        /// 设置配置文件
        /// </summary>
        /// <param name="section"></param>
        public static void SetAppSetting(IConfigurationSection section)
        {
            appSection = section;
        }

        /// <summary>
        /// 加载存货档案model
        /// </summary>
        public static API_Inventory LoadInventoryJson()
        {
            using (StreamReader r = new StreamReader("InvModel.json"))
            {
                string json = r.ReadToEnd();
                API_Inventory items = JsonConvert.DeserializeObject<API_Inventory>(json);
                return items;
            }
        }
        /// <summary>
        /// 加载客户档案model
        /// </summary>
        public static AS_Customer LoadCustomerJson()
        {
            using (StreamReader r = new StreamReader("CusModel.json"))
            {
                string json = r.ReadToEnd();
                AS_Customer items = JsonConvert.DeserializeObject<AS_Customer>(json);
                return items;
            }
        }
        /// <summary>
        /// 加载供应商档案model
        /// </summary>
        public static AS_Vendor LoadVenJson()
        {
            using (StreamReader r = new StreamReader("VenModel.json"))
            {
                string json = r.ReadToEnd();
                AS_Vendor items = JsonConvert.DeserializeObject<AS_Vendor>(json);
                return items;
            }
        }
    }
}
